package com.liu.springmvc.handler;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author LIU
 * @version 1.0
 * @date 2024/1/23 14:56
 * @description Bean名称 映射器
 */
@Component
public class MyBeanNameUrlHandlerMapping extends AbstractHandlerMapping implements BeanPostProcessor {

    static Map<String,Object> map =  new HashMap();


    @Override
    protected Object getHandlerInternal(HttpServletRequest request) {
        return map.get(request.getRequestURI());
    }


    /**
     * 找到所有的映射器
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

        if (beanName.startsWith("/")){
            System.out.println("添加beanNameUrl--->"  + beanName + " "+ bean);
            map.put(beanName,bean);
        }
        return bean;
    }
}
